package com.eway.viewanimator;

import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AtoZDemo extends AppCompatActivity implements View.OnClickListener {

	private TextView chartShow;
	private Button chartStart;
	private Button circleStart;
	private ImageView circleImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ato_zdemo);


		chartShow = (TextView) findViewById(R.id.chart_show);
		chartStart = (Button) findViewById(R.id.chart_start);
		circleStart = findViewById(R.id.circle_start);
		circleImage = findViewById(R.id.circle);

		chartStart.setOnClickListener(this);
		circleStart.setOnClickListener(this);


	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.chart_show:

				break;
			case R.id.chart_start:
				ValueObjectAToZ();
				break;
			case R.id.circle_start:
				circle();


				break;
			default:
				break;
		}

	}


	/**
	 * 字母的改变的
	 */
	private void ValueObjectAToZ() {

		ValueAnimator valueAnimator = ValueAnimator.ofObject(new ChartEvaluator(), new Character('A'), new Character('Z'));
		valueAnimator.setDuration(1000);
//		valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
		valueAnimator.setInterpolator(new LinearInterpolator());

		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				char charValue = (char) animation.getAnimatedValue();
				String tv = String.valueOf(charValue);
				chartShow.setText(tv);


			}
		});


		valueAnimator.start();


	}


	/**
	 * 圆的抛物线
	 */
	protected void circle() {
		ValueAnimator circleValueAnimator = ValueAnimator.ofObject(new CircleEvalutor(), new Point(circleImage.getLeft(), circleImage.getTop()), new Point(circleImage.getLeft() + 300, circleImage.getTop() + 300));
		circleValueAnimator.setDuration(4000);
		circleValueAnimator.setInterpolator(new LinearInterpolator());

		circleValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				Point point = (Point) animation.getAnimatedValue();
				circleImage.layout(point.x, point.y, point.x + circleImage.getWidth(), point.y + circleImage.getHeight());
			}
		});

		circleValueAnimator.start();
	}

	/**
	 * 字母的A-Z
	 */
	protected class ChartEvaluator implements TypeEvaluator<Character> {


		@Override
		public Character evaluate(float fraction, Character startValue, Character endValue) {
			int mStartValue = (int) startValue;
			int mEndValue = (int) endValue;

			int cureentValue = (int) (mStartValue + fraction * (mEndValue - mStartValue));

			char returnValue = (char) cureentValue;

			return returnValue;
		}
	}

	/**
	 * Circle的拦截器
	 */
	protected class CircleEvalutor implements TypeEvaluator<Point> {


		private int currentY;

		@Override
		public Point evaluate(float fraction, Point startValue, Point endValue) {

			int startX = startValue.x;
			int startY = startValue.y;
			int endX = endValue.x;
			int endY = endValue.y;

			int currentX = (int) (startX + fraction * (endX - startX));
			if (fraction <= 0.5) {
				currentY = (int) (startY + fraction * 2 * (endY - startY));
			} else {
//				currentY = endY;
				currentY= (int) (endY-Math.random()*(100));
			}

			return new Point(currentX, currentY);
		}
	}

}
